package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

@RestController
@RequestMapping(value = "/admin/report")
@Api(tags = "数据统计相关功能")
@Slf4j
public class ReportController {
    // 注入Service层对象
    @Autowired
    private ReportService reportService;

    /*
     * 营业额统计方法
     * */
    @GetMapping(value = "/turnoverStatistics")
    @ApiOperation(value = "营业额统计方法")
    public Result<TurnoverReportVO> turnoverStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("营业额统计方法,begin:{},end{}", begin, end);
        return Result.success(reportService.getTurnoverStatistics(begin, end)); // 传入参数并获取日期与日期营业额
    }

    /*
     *用户人数统计方法
     * */
    @GetMapping(value = "/userStatistics")
    @ApiOperation(value = "用户数量统计方法")
    public Result<UserReportVO> userStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("用户数量统计方法,begin:{},end{}", begin, end);
        return Result.success(reportService.getUserStatistics(begin, end));
    }

    /*
     * 订单数统计功能， 订单数 有效订单数，订单完成率
     * */
    @GetMapping(value = "/ordersStatistics")
    @ApiOperation(value = "订单统计方法")
    public Result<OrderReportVO> ordersStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("订单统计方法,begin:{},end{}", begin, end);
        return Result.success(reportService.getrOdersStatistics(begin, end));
    }

    /*
     * 统计TOP10商品方法
     * */
    @GetMapping(value = "/top10")

    public Result<SalesTop10ReportVO> getTop10(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("统计TOP10商品方法,begin:{},end{}", begin, end);
        return Result.success(reportService.getSalesTop10(begin, end));
    }
}
